using System;
using System.Windows.Forms;
using FBroSharp.Lib;
using FBroSharp.Event;
using FBroSharp.DataType;
using FBroSharp.Value;
using FBroSharp.Const;

namespace 多标签页浏览器
{
    /// <summary>
    /// 浏览器事件处理器 - 每个标签页都有独立的事件处理器实例
    /// </summary>
    public class BrowserEvent : FBroSharpBrowserEvent
    {
        private Form1 _form;
        private TabPage _tabPage;

        public BrowserEvent(Form1 form, TabPage tabPage)
        {
            _form = form;
            _tabPage = tabPage;
        }

        /// <summary>
        /// ⚠️ REQUIRED: 浏览器创建完成（TWO parameters!）
        /// </summary>
        public override void OnAfterCreated(IFBroSharpBrowser browser, IFBroSharpDictionaryValue extrainfo)
        {
            _form.OnBrowserCreated(browser, _tabPage);
        }

        /// <summary>
        /// ⚠️ CRITICAL: 弹窗控制 - 在当前窗口打开而不是新窗口
        /// </summary>
        public override bool OnBeforePopup(
            IFBroSharpBrowser browser, 
            IFBroSharpFrame frame, 
            string target_url, 
            string target_frame_name,
            FBroSharpWindowOpenDisposition target_disposition, 
            bool user_gesture, 
            FBroSharpPopupfeatures popupFeatures, 
            ref FBroSharpWindowsInfo windowInfo,
            ref FBroSharpBrowserSetting settings,
            ref bool no_javascript_access, 
            IFBroSharpUseExtraData user_settings)
        {
            // 在当前标签页打开链接
            frame.LoadURL(target_url);
            return true; // true = 阻止弹窗
        }

        /// <summary>
        /// 地址变化
        /// </summary>
        public override void OnAddressChange(IFBroSharpBrowser browser, IFBroSharpFrame frame, string url)
        {
            if (frame.IsMain())
            {
                _form.UpdateAddressBar(_tabPage, url);
            }
        }

        /// <summary>
        /// 标题变化
        /// </summary>
        public override void OnTitleChange(IFBroSharpBrowser browser, string title)
        {
            _form.UpdateTabTitle(_tabPage, title);
        }

        /// <summary>
        /// 加载状态变化
        /// </summary>
        public override void OnLoadingStateChange(IFBroSharpBrowser browser, bool isLoading, bool canGoBack, bool canGoForward)
        {
            _form.UpdateNavigationButtons(_tabPage, isLoading, canGoBack, canGoForward);
        }

        /// <summary>
        /// 页面加载完成
        /// </summary>
        public override void OnLoadEnd(IFBroSharpBrowser browser, IFBroSharpFrame frame, int httpStatusCode)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus(_tabPage, $"加载完成 (HTTP {httpStatusCode})");
            }
        }

        /// <summary>
        /// 页面加载错误（errorCode is int!）
        /// </summary>
        public override void OnLoadError(IFBroSharpBrowser browser, IFBroSharpFrame frame, 
            int errorCode, string errorText, string failedUrl)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus(_tabPage, $"加载失败: {errorText}");
            }
        }

        /// <summary>
        /// 浏览器即将关闭
        /// </summary>
        public override void OnBeforeClose(IFBroSharpBrowser browser)
        {
            Console.WriteLine($"浏览器即将关闭: {browser.GetIdentifier()}");
        }

        /// <summary>
        /// 是否允许浏览器关闭
        /// </summary>
        public override bool DoClose(IFBroSharpBrowser browser)
        {
            return false; // false = 允许关闭
        }
    }
}

